// mine/pages/recipesadd/index.js
const $api = require("../../../api/mine").API
const $upload = require("../../../api/upload").API

Page({

  /**
   * 页面的初始数据
   */
  data: {
    false:false,
    true:true,
    show:false,
    child_dietime:"",
    child_diedesc:"",
    columns: [],
    columns1: [],
    caipin:[
      {
        name:"",
        img:[]
      }
    ],
    showsettime:false,
    child_dietime:"",
    child_diedesc:"",
    child_dietime_desc:"",
    week_desc:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var t = this
    t.getWeekType()
    t.getDietType()
  },
  settime(e){
    console.log(2222)
    var t = this
    t.setData({
      showsettime:true
    })
  },
  closetime(e){
    var t = this
    t.setData({
      showsettime:false
    })
  },
  choosetime(e){
    var t = this
    console.log(e)
    t.setData({
      week:e.detail.value.dict_key,
      week_desc:e.detail.value.dict_value,
      showsettime:false
    })
  },
  addcaipin(e){
    var t = this
    var lists = t.data.caipin
    var item = {
      name:"",
      img:[]
    }
    lists.push(item)
    t.setData({
      caipin:lists
    })
  },
  setchild_dietime(e){
    var t = this
    t.setData({
      showchild_dietime:true
    })
  },
  closechild_dietime(e){
    var t = this
    t.setData({
      showchild_dietime:false
    })
  },
  choosechild_dietime(e){
    var t = this
    console.log(e)
    t.setData({
      child_dietime:e.detail.value.dict_key,
      child_dietime_desc:e.detail.value.dict_value,

      showchild_dietime:false
    })
  },
  onChangechild_diedesc(e){
    var t = this
    t.setData({
      child_diedesc:e.detail
    })
  },
  afterRead(e){
    var t = this,
    index = e.currentTarget.dataset.index
    var caipin = t.data.caipin
    caipin[index].img.push(e.detail.file)
    t.setData({
      caipin:caipin
    })
  },
  getWeekType(){
    var t = this
    var columns = wx.getStorageSync('week')
    if(columns){
      t.setData({
        columns:columns
      })
      return false
    }
    $api.getWeekType().then(res=>{
      wx.setStorageSync('week', res.data)
      t.setData({
        columns:res.data
      })
    })
  },
  getDietType(){
    var t = this
    var columns1 = wx.getStorageSync('DietTypeList')
    if(columns1){
      t.setData({
        columns1:columns1
      })
      return false
    }
    $api.getDietType().then(res=>{
      wx.setStorageSync('DietTypeList',res.data)
      t.setData({
        columns1:res.data
      })
    })
  },
  // 发布食谱
  saveTeacherDiet() {
    var t = this
    var uploads = []
    t.data.caipin.forEach((item,i)=>{
      uploads[i] =new Promise ((resolve)=>{
        $upload.uploadQiniu(item.img).then(res=>{
          var items = {
            file_path:res[0].file_path,
            file_size:res[0].file_size,
            file_type:item.img[0].type,
            file_extension_name:res[0].file_extension_name,
            sys_file_desc:item.name
          }
          resolve(items)
        })
      })
    })
    Promise.all(uploads).then(res=>{
      var data = {
        member_id:wx.getStorageSync('memberinfo').member_id,
        institution_id:wx.getStorageSync('TeacherClassinfo').institution_id,
        grade_id:wx.getStorageSync('TeacherClassinfo').grade_id,
        class_id:wx.getStorageSync('TeacherClassinfo').class_id,
        week:t.data.week,
        child_dietime:t.data.child_dietime,
        child_diedesc:t.data.child_diedesc,
        circle_files:res,
      }
      $api.saveTeacherDiet(data).then(res=>{
        console.log(res)
        wx.showToast({
          title: '发布成功',
        })
        wx.navigateBack({
          delta: 1,
        })
      })
    })
    
    
  },
  // 获取菜品名称
  onChangeimgname(e){
    var t = this,
    index = e.currentTarget.dataset.index
    var caipin = t.data.caipin
    caipin[index].name = e.detail
    console.log(caipin[index].name)
    t.setData({
      caipin:caipin
    })
  }
})